<template>
  <div class="roles-wrapper">
    <el-table v-loading="listLoading" :data="list" size="mini">
      <el-table-column prop="id" label="角色id" align="center" width="80" />
      <el-table-column label="角色名">
        <template slot-scope="{row}">
          <div v-if="row.edit" style="display:flex">
            <el-input v-model="row.role_name" size="mini" />
            <el-button
              size="mini"
              class="b-cancel"
              icon="el-icon-refresh-left"
              type="warning"
              @click="row.role_name = row.rawName, row.edit = false;"
            >取消</el-button>
          </div>
          <span v-else>{{ row.role_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="right">
        <el-button slot="header" size="mini" type="info" icon="el-icon-plus" @click="addHandler">添加</el-button>
        <template slot-scope="{$index,row}">
          <el-button
            v-if="row.edit"
            size="mini"
            icon="el-icon-delete"
            type="danger"
            @click="deleteRole(row,$index)"
          >删除</el-button>
          <el-button
            size="mini"
            :icon="row.edit?'el-icon-finished':'el-icon-edit-outline'"
            :type="row.edit?'success':'primary'"
            @click="confirmEdit(row)"
          >{{ row.edit?'提交':'编辑' }}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// api
import { getRoleList, editRoleName, createRole, deleteRole } from '@/api/user';

export default {
  data() {
    return {
      listLoading: true,
      list: null,
      createForm: {
        role_name: '',
      },
    };
  },
  async mounted() {
    await this.getRoleList();
  },
  methods: {
    async getRoleList() {
      this.listLoading = true;
      const res = await getRoleList();
      const list = res.data.rows || res.data;
      this.list = list.map((v) => {
        this.$set(v, 'edit', false);
        this.$set(v, 'rawName', v.role_name);
        return v;
      });
      this.listLoading = false;
    },
    async confirmEdit(row) {
      if (!row.edit) {
        row.edit = true;
        return;
      }
      if (row.role_name === row.rawName) {
        row.edit = false;
        return;
      }
      const { code, message } = await editRoleName({
        id: row.id,
        role_name: row.role_name,
      });
      this.$message({
        type: code === 200 ? 'success' : 'error',
        message,
      });
      if (code !== 200) return;
      row.edit = false;
      row.rawName = row.name;
    },
    async addHandler() {
      this.$prompt('请输入角色名', '添加新角色', {
        showCancelButton: false,
      })
        .then(({ value }) => createRole({ role_name: value }))
        .then(({ code, data, message }) => {
          this.$message({
            type: code === 200 ? 'success' : 'error',
            message,
          });
          if (code !== 200) return;
          this.formatData(data);
          this.list.push(data);
        })
        .catch(() => this.$message.error('取消操作'));
    },
    formatData(data) {
      data.edit = false;
      data.rawName = data.name;
    },
    async deleteRole(row, index) {
      const { code, message } = await deleteRole({ id: row.id });
      this.$message({
        type: code === 200 ? 'success' : 'error',
        message,
      });
      if (code !== 200) return;
      this.list.splice(index, 1);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.roles-wrapper {
  width: $baseWidth;
  margin: auto;
}
.b-cancel {
  margin-left: 10px;
}
</style>
